drop table jms_dwd.dwd_yl_oms_oms_waybill_incre_dt;
CREATE EXTERNAL TABLE jms_dwd.dwd_yl_oms_oms_waybill_incre_dt(
  `id` bigint COMMENT '主键id',
  `waybill_no` string COMMENT '运单号',
  `waybill_status_code` bigint COMMENT '运单状态code',
  `goods_type_id` bigint COMMENT '物品类型id',
  `goods_type_code` string COMMENT '物品类型code',
  `goods_type_name` string COMMENT '物品类型名称',
  `goods_name` string COMMENT '物品名称',
  `package_length` double COMMENT '包裹总长,单位厘米',
  `package_wide` double COMMENT '包裹总宽,单位厘米',
  `package_high` double COMMENT '包裹总高,单位厘米',
  `package_volume` double COMMENT '包裹体积重,单位千克',
  `package_total_volume` decimal(14,2) COMMENT '包裹总体积,单位立方厘米',
  `package_charge_weight` decimal(14,2) COMMENT '包裹计费重量,单位千克',
  `collect_staff_code` string COMMENT '揽件业务员code',
  `collect_time` timestamp COMMENT '揽件时间',
  `dispatch_staff_code` string COMMENT '派件业务员code',
  `dispatch_staff_name` string COMMENT '派件业务员名称',
  `dispatch_time` timestamp COMMENT '派件时间',
  `dispatch_network_code` string COMMENT '派件网点code',
  `dispatch_network_name` string COMMENT '派件网点名称',
  `exception_code` string COMMENT '异常原因编码',
  `terminal_dispatch_code` string COMMENT '三段码',
  `first_code` string COMMENT '一段码',
  `second_code` string COMMENT '二段码',
  `third_code` string COMMENT '三段码',
  `is_sign` smallint COMMENT '签收标识,1是，0否',
  `sign_network_code` string COMMENT '签收网点code',
  `sign_network_name` string COMMENT '签收网点名称',
  `sign_finance_code` string COMMENT '签收财务网点编码',
  `sign_time` timestamp COMMENT '签收时间',
  `is_abnormal` smallint COMMENT '问题件标识,1是，0否',
  `abnormal_reg_network_code` string COMMENT '问题件登记网点code',
  `abnormal_reg_time` timestamp COMMENT '问题件登记时间',
  `customer_order_id` string COMMENT '客户订单编号',
  `waybill_source_code` string COMMENT '运单来源code',
  `waybill_source_name` string COMMENT '运单来源名称',
  `is_void` smallint COMMENT '是否作废件,1是，0否',
  `is_refund` smallint COMMENT '1:已退件2:已转寄3:未退件4:退转件',
  `customer_code` string COMMENT '客户编号code',
  `customer_name` string COMMENT '客户编号名称',
  `sender_name` string COMMENT '寄件人姓名',
  `sender_province_id` bigint COMMENT '寄件省份id',
  `sender_city_id` bigint COMMENT '寄件城市id',
  `sender_area_id` bigint COMMENT '寄件区域Id',
  `sender_township` string COMMENT '寄件乡镇',
  `sender_street` string COMMENT '寄件街道',
  `sender_detailed_address` string COMMENT '寄件详细地址',
  `origin_id` bigint COMMENT '始发地id',
  `pick_network_code` string COMMENT '寄件网点code',
  `pick_network_name` string COMMENT '寄件网点名称',
  `freight` decimal(14,2) COMMENT '运费',
  `total_freight` decimal(14,2) COMMENT '总运费',
  `pick_finance_code` string COMMENT '寄件财务中心code',
  `dispatch_finance_code` string COMMENT '派件财务中心code',
  `receiver_name` string COMMENT '收件人姓名',
  `receiver_province_id` bigint COMMENT '收件省份id',
  `receiver_city_id` bigint COMMENT '收件城市id',
  `receiver_area_id` bigint COMMENT '收���区域id',
  `receiver_township` string COMMENT '收件乡镇',
  `receiver_street` string COMMENT '收件街道',
  `receiver_detailed_address` string COMMENT '收件详细地址',
  `destination_id` bigint COMMENT '目的地id',
  `receiver_sorting_code` string COMMENT '收件分拣码',
  `input_time` timestamp COMMENT '录入时间',
  `input_network_code` string COMMENT '录入网点code',
  `settlement_weight` decimal(14,2) COMMENT '结算重量',
  `order_source_code` string COMMENT '订单来源编码',
  `last_update_time` timestamp COMMENT '最后更新时间',
  `is_delete` smallint COMMENT '是否删除,1未删除，2已删除',
  `package_collect_weight` double COMMENT '收件重量,单位千克',
  `package_receipt_weight` double COMMENT '入仓重量,单位千克',
  `package_arrival_weight` double COMMENT '集散到件重量,单位千克',
  `package_inside_charge_weight` decimal(14,2) COMMENT '内部计费重量,单位千克',
  `waybill_weight` double COMMENT '运单重量,单位千克',
  `is_business` smallint COMMENT '是否是商务件：0否，1是',
  `is_receive` smallint COMMENT '是否已收款,1是，0否',
  `express_type_code` string COMMENT '产品类型code',
  `express_type_name` string COMMENT '产品类型名称',
  `pick_finance_name` string COMMENT '寄件财务中心名称',
  `settlement_code` string COMMENT '结算方式编码',
  `settlement_name` string COMMENT '结算方式名称',
  `customer_type` bigint COMMENT '客户所属：1-总部共享,2-代理区共享,3-自有客户',
  `center_name` string COMMENT '转运中心名称',
  `center_code` string COMMENT '转运中心编码',
  `ordersource_code` string COMMENT '多平台订单来源编码',
  `ordersource_name` string COMMENT '多平台订单来源名称',
  `etl_load_time` timestamp COMMENT '数据录入hdfs时间',
  `package_total_weight` decimal(14,2) COMMENT '包裹总重量,单位千克',
  `receiver_mobile_phone` string COMMENT '收件人手机号')
COMMENT '运单去重记录表,每个分区保留的是当日新增数据'
PARTITIONED BY (
  `dt` string COMMENT '运单录入日期(yyyy-MM-dd)')
STORED AS PARQUET
LOCATION '/dw/hive/jms_dwd.db/external/dwd_yl_oms_oms_waybill_incre_dt'
TBLPROPERTIES (
    'discover.partitions' = 'false',
    'parquet.column.index.access' = 'true'
);

msck repair table jms_dwd.dwd_yl_oms_oms_waybill_incre_dt;

alter table jms_dwd.dwd_yl_oms_oms_waybill_incre_dt add columns(DELIVERY_TIME timestamp comment'寄件时间') cascade;

alter table jms_dwd.dwd_yl_oms_oms_waybill_incre_dt add columns(
 dispatch_network_id             bigint      COMMENT '派件网点id',
 pick_network_id                 bigint      COMMENT '寄件网点id'
) cascade;




-- 手动补数脚本
with first_code_date as (
    select code,center_name,center_code
    from jms_dim.dim_lmdm_sys_first_code
    where is_enable=1 and is_delete = 1
    group by code,center_name,center_code
)

,tmp_tab as (
    select
        id
        ,waybill_no
        ,waybill_status_code
        ,goods_type_id
        ,goods_type_code
        ,goods_type_name
        ,goods_name
        ,cast(package_length AS DECIMAL(14,2)) as package_length
        ,cast(package_wide AS DECIMAL(14,2)) as package_wide
        ,cast(package_high AS DECIMAL(14,2)) as package_high
        ,cast(package_volume AS DECIMAL(14,2)) as package_volume
        ,cast(package_total_volume AS DECIMAL(14,2)) as package_total_volume
        ,cast(package_charge_weight AS DECIMAL(14,2)) as package_charge_weight
        ,collect_staff_code
        ,collect_time
        ,dispatch_staff_code
        ,dispatch_staff_name
        ,dispatch_time
        ,dispatch_network_code
        ,dispatch_network_name
        ,exception_code
        ,terminal_dispatch_code
        ,nullif(split(terminal_dispatch_code, ',')[0], '') AS first_code
        ,nullif(split(terminal_dispatch_code, ',')[1], '') AS second_code
        ,nullif(split(terminal_dispatch_code, ',')[2], '') AS third_code
        ,is_sign
        ,sign_network_code
        ,sign_network_name
        ,sign_finance_code
        ,sign_time
        ,is_abnormal
        ,abnormal_reg_network_code
        ,abnormal_reg_time
        ,customer_order_id
        ,waybill_source_code
        ,waybill_source_name
        ,is_void
        ,is_refund
        ,customer_code
        ,customer_name
        ,sender_name
        ,sender_province_id
        ,sender_city_id
        ,sender_area_id
        ,sender_township
        ,sender_street
        ,sender_detailed_address
        ,origin_id
        ,pick_network_code
        ,pick_network_name
        ,cast(freight AS DECIMAL(14,2)) as freight
        ,cast(total_freight AS DECIMAL(14,2)) as total_freight
        ,pick_finance_code
        ,dispatch_finance_code
        ,receiver_name
        ,receiver_province_id
        ,receiver_city_id
        ,receiver_area_id
        ,receiver_township
        ,receiver_street
        ,receiver_detailed_address
        ,destination_id
        ,receiver_sorting_code
        ,input_time
        ,input_network_code
        ,cast(settlement_weight AS DECIMAL(14,2)) as settlement_weight
        ,order_source_code
        ,last_update_time
        ,is_delete
        ,cast(package_collect_weight AS DECIMAL(14,2)) as  package_collect_weight
        ,cast(package_receipt_weight AS DECIMAL(14,2)) as  package_receipt_weight
        ,cast(package_arrival_weight AS DECIMAL(14,2)) as  package_arrival_weight
        ,cast(package_inside_charge_weight AS DECIMAL(14,2)) as  package_inside_charge_weight
        ,waybill_weight
        ,is_business
        ,is_receive
        ,express_type_code
        ,express_type_name
        ,pick_finance_name
        ,settlement_code
        ,settlement_name
        ,customer_type
        ,null center_name
        ,null center_code
        ,subscribe_source_code as ordersource_code
        ,subscribe_source_name as ordersource_name
        ,input_time as etl_load_time
        ,cast(package_total_weight AS DECIMAL(14,2)) as package_total_weight
        ,receiver_mobile_phone
        ,delivery_time
        ,dispatch_network_id
        ,pick_network_id
        ,inspection_method
        ,package_number
        ,box_standard_code
        ,box_standard_name
        ,box_number
        ,box_price
        ,collect_staff_name
        ,exception_reason
        ,sign_network_id
        ,sign_finance_name
        ,is_real_name
        ,id_no
        ,abnormal_reg_network_name
        ,order_id
        ,send_code
        ,send_name
        ,dispatch_code
        ,dispatch_name
        ,is_need_receipt
        ,receipt_no
        ,is_settlement
        ,settlement_time
        ,sender_mobile_phone
        ,sender_telphone
        ,sender_country_id
        ,sender_country_name
        ,sender_province_name
        ,sender_city_name
        ,sender_area_name
        ,sender_postal_code
        ,sender_email
        ,origin_code
        ,origin_name
        ,package_cost
        ,tax
        ,handicraft_fee
        ,other_fee
        ,receive_pay_fee
        ,pick_finance_id
        ,dispatch_finance_name
        ,express_type_id
        ,insured
        ,insured_amount
        ,insured_fee
        ,cod_need
        ,cod_money
        ,cod_fee
        ,settlement_id
        ,paid_mode_code
        ,paid_mode_name
        ,receiver_company
        ,receiver_telphone
        ,receiver_country_id
        ,receiver_country_name
        ,receiver_province_name
        ,receiver_city_name
        ,receiver_area_name
        ,receiver_postal_code
        ,receiver_email
        ,destination_code
        ,destination_name
        ,input_staff_code
        ,input_staff_name
        ,input_network_name
        ,coupon_code
        ,coupon_amount
        ,remarks
        ,prints_number
        ,bill_no
        ,is_cash
        ,is_cod_receive
        ,currency_code
        ,currency_name
        ,customer_id
        ,is_plaintext
        ,refund_time
        ,input_staff_by
        ,real_name
        ,id_no_type
        ,sex
        ,distribute_finance_id
        ,distribute_finance_code
        ,distribute_finance_name
        ,is_distribute_scan
        ,is_arrival_scan
        ,miss_flag
        ,customer_network_code
        ,customer_network_name
        ,calculate_fee_desc
        ,calculate_fee_code
        ,printer_counterfoil
        ,quotetype_code
        ,receipt_fee
        ,package_center_arrival_weight
        ,date_format(input_time, 'yyyy-MM-dd') as dt
        ,row_number() over(partition by waybill_no order by input_time desc) as row_id
    from jms_ods.yl_oms_oms_waybill
    where dt >= date_add('2023-01-29', -60)
    and date_format(input_time,'yyyy-MM-dd') >= date_add('2023-01-29', -60)
)

insert overwrite table bidefault.dwd_yl_oms_oms_waybill_incre_dt partition(dt)
select
    id
    ,waybill_no
    ,waybill_status_code
    ,goods_type_id
    ,goods_type_code
    ,goods_type_name
    ,goods_name
    ,package_length
    ,package_wide
    ,package_high
    ,package_volume
    ,package_total_volume
    ,package_charge_weight
    ,collect_staff_code
    ,collect_time
    ,dispatch_staff_code
    ,dispatch_staff_name
    ,dispatch_time
    ,dispatch_network_code
    ,dispatch_network_name
    ,exception_code
    ,terminal_dispatch_code
    ,first_code
    ,second_code
    ,third_code
    ,is_sign
    ,sign_network_code
    ,sign_network_name
    ,sign_finance_code
    ,sign_time
    ,is_abnormal
    ,abnormal_reg_network_code
    ,abnormal_reg_time
    ,customer_order_id
    ,waybill_source_code
    ,waybill_source_name
    ,is_void
    ,is_refund
    ,customer_code
    ,customer_name
    ,sender_name
    ,sender_province_id
    ,sender_city_id
    ,sender_area_id
    ,sender_township
    ,sender_street
    ,sender_detailed_address
    ,origin_id
    ,pick_network_code
    ,pick_network_name
    ,freight
    ,total_freight
    ,pick_finance_code
    ,dispatch_finance_code
    ,receiver_name
    ,receiver_province_id
    ,receiver_city_id
    ,receiver_area_id
    ,receiver_township
    ,receiver_street
    ,receiver_detailed_address
    ,destination_id
    ,receiver_sorting_code
    ,input_time
    ,input_network_code
    ,settlement_weight
    ,order_source_code
    ,last_update_time
    ,is_delete
    ,package_collect_weight
    ,package_receipt_weight
    ,package_arrival_weight
    ,package_inside_charge_weight
    ,waybill_weight
    ,is_business
    ,is_receive
    ,express_type_code
    ,express_type_name
    ,pick_finance_name
    ,settlement_code
    ,settlement_name
    ,customer_type
    ,t2.center_name
    ,t2.center_code
    ,ordersource_code
    ,ordersource_name
    ,etl_load_time
    ,package_total_weight
    ,receiver_mobile_phone
    ,delivery_time
    ,dispatch_network_id
    ,pick_network_id
    ,inspection_method
    ,package_number
    ,box_standard_code
    ,box_standard_name
    ,box_number
    ,box_price
    ,collect_staff_name
    ,exception_reason
    ,sign_network_id
    ,sign_finance_name
    ,is_real_name
    ,id_no
    ,abnormal_reg_network_name
    ,order_id
    ,send_code
    ,send_name
    ,dispatch_code
    ,dispatch_name
    ,is_need_receipt
    ,receipt_no
    ,is_settlement
    ,settlement_time
    ,sender_mobile_phone
    ,sender_telphone
    ,sender_country_id
    ,sender_country_name
    ,sender_province_name
    ,sender_city_name
    ,sender_area_name
    ,sender_postal_code
    ,sender_email
    ,origin_code
    ,origin_name
    ,package_cost
    ,tax
    ,handicraft_fee
    ,other_fee
    ,receive_pay_fee
    ,pick_finance_id
    ,dispatch_finance_name
    ,express_type_id
    ,insured
    ,insured_amount
    ,insured_fee
    ,cod_need
    ,cod_money
    ,cod_fee
    ,settlement_id
    ,paid_mode_code
    ,paid_mode_name
    ,receiver_company
    ,receiver_telphone
    ,receiver_country_id
    ,receiver_country_name
    ,receiver_province_name
    ,receiver_city_name
    ,receiver_area_name
    ,receiver_postal_code
    ,receiver_email
    ,destination_code
    ,destination_name
    ,input_staff_code
    ,input_staff_name
    ,input_network_name
    ,coupon_code
    ,coupon_amount
    ,remarks
    ,prints_number
    ,bill_no
    ,is_cash
    ,is_cod_receive
    ,currency_code
    ,currency_name
    ,customer_id
    ,is_plaintext
    ,refund_time
    ,input_staff_by
    ,real_name
    ,id_no_type
    ,sex
    ,distribute_finance_id
    ,distribute_finance_code
    ,distribute_finance_name
    ,is_distribute_scan
    ,is_arrival_scan
    ,miss_flag
    ,customer_network_code
    ,customer_network_name
    ,calculate_fee_desc
    ,calculate_fee_code
    ,printer_counterfoil
    ,quotetype_code
    ,receipt_fee
    ,package_center_arrival_weight
    ,dt
from tmp_tab t1
left join first_code_date t2
on t1.first_code = t2.code
where t1.row_id = 1
distribute by dt,pmod(hash(rand()),80);